package com.demo.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.demo.model.Dog;
import com.demo.model.User;
import com.demo.service.UserService;
import com.demo.serviceImpl.UserServiceImpl;

public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		UserService service = new UserServiceImpl();
		String type = request.getParameter("type");
		
		//对用户的操作
		if("getAlluser".equals(type)){
			getAllUser(request, response);
		}
		
		if("del".equals(type)){
			String userId = request.getParameter("userId");
			int i = service.delUser(userId);
			
			if(i!=0){
				getAllUser(request, response);
			}else{
				//赋值
				request.setAttribute("mes", "删除失败");
				request.getRequestDispatcher("error.jsp").forward(request, response);
			}
		}
		
		if("selOne".equals(type)){
			String userId = request.getParameter("userId");
			User user = service.selOne(userId);
			
			if(user!=null){
				request.setAttribute("user", user);
				request.getRequestDispatcher("singleUser.jsp").forward(request, response);
			}else{
				request.setAttribute("mes", "操作失败！");
			    request.getRequestDispatcher("error.jsp").forward(request, response);
			}
		}
		//对宠物的操作
		if("getAlldog".equals(type)){
			getAllDog(request, response);
		}
		
		if("delDog".equals(type)){
			String userId = request.getParameter("userId");
			int i = service.delDog(userId);
			if(i>0){
				getAllDog(request, response);
			}else{
				request.setAttribute("mes","删除失败");
				request.getRequestDispatcher("error.jsp").forward(request, response);
			}
		}
		
		if("selOnedog".equals(type)){
			String userId = request.getParameter("userId");
			Dog dog = service.selOnedog(userId);
			if(dog!=null){
				request.setAttribute("dog",dog);
				request.getRequestDispatcher("singleDog.jsp").forward(request, response);
			}else{
				request.setAttribute("mes","操作失败！");
				request.getRequestDispatcher("error.jsp").forward(request, response);
			}
			
		}
		
}

	//获取所有用户信息
	public void getAllUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		UserService service = new UserServiceImpl();
		List<User> list = service.getAlluser();
		
		//将从数据库查询到的用户信息返回给列表页面并加载
		request.setAttribute("list", list);
		request.getRequestDispatcher("userlist.jsp").forward(request, response);
	}
	
	//获取所有宠物信息
	public void getAllDog(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		UserService service = new UserServiceImpl();
		List<Dog> list = service.getAlldog();
		
		//将从数据库中查询到的宠物信息返回给列表页面并加载
		request.setAttribute("list", list);
		request.getRequestDispatcher("doglist.jsp").forward(request, response);
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		UserService service = new UserServiceImpl();
		
		String type = request.getParameter("type");
		//用户登录
		if ("user_login".equals(type)) {
			// 接收页面user_login.jsp的参数
			String username = request.getParameter("userName");
			String pwd = request.getParameter("userPwd");

			// 将接收到的用户参数封装到model类中
			User user = new User(username, pwd);

			
			User newUser = service.login(user);

			if (newUser != null) {
				request.getRequestDispatcher("user.jsp").forward(request, response);
			} else {
				request.getRequestDispatcher("error.jsp").forward(request, response);
			}

		}
		//管理员登录
		if("manager_login".equals(type)){
			String username = request.getParameter("userName");
			String pwd = request.getParameter("userPwd");
			User user = new User(username, pwd);
			User newUser = service.login1(user);
			
			if(newUser!=null){
				request.getRequestDispatcher("manager.jsp").forward(request, response);
			}else{
				request.getRequestDispatcher("error.jsp").forward(request, response);
			}
			
		}
		//用户注册
		if("regist".equals(type)){
			//接收页面regist.jsp的参数
			String username = request.getParameter("userName");
			String pwd = request.getParameter("userPwd");
			String sex = request.getParameter("sex");
			String email = request.getParameter("email");
			String address = request.getParameter("address");
			String mobilephone = request.getParameter("mobilephone");

				//将接收到的用户参数封装到model类中
				User user = new User(username, pwd, sex, email, address, mobilephone);
	            
	            int i  = service.regist(user);
	            if (i!=0) {
					request.setAttribute("temp_msg", "注册成功，请登录！！！");
					request.setAttribute("temp_name", username);
					request.getRequestDispatcher("registsuss.jsp").forward(request, response);
				} else {	
					request.setAttribute("temp_msg", "注册失败！！！");
					request.getRequestDispatcher("regist.jsp").forward(request, response);
				}
			}
		//增加宠物信息
        if("addOnedog".equals(type)){
        	//接收页面add_Dog.jsp的参数
			String uname = request.getParameter("username");
			String age = request.getParameter("age");
			String birthday = request.getParameter("birthday");
			String type2 = request.getParameter("type2");
			String color = request.getParameter("color");
			String pic = request.getParameter("pic");
			String body_length = request.getParameter("body_length");
			String body_weight = request.getParameter("body_weight");
			
			Dog dog = new Dog(uname, age, birthday, type2, color, pic, body_length, body_weight);
			
			int i = service.addOnedog(dog);
			
			if(i>0){
				getAllDog(request, response);
			}else{
				request.setAttribute("mes", "添加失败");
				request.getRequestDispatcher("error.jsp").forward(request, response);
			}
		}
        //修改用户信息
		if("upd".equals(type)){
			String uid = request.getParameter("userId");
			String uname = request.getParameter("username");
			String upwd = request.getParameter("userpwd");
			String sex = request.getParameter("sex");
			String email = request.getParameter("email");
			String address = request.getParameter("address");
			String mobilephone = request.getParameter("mobilephone");
			
			//把用户参数封装到user对象中
			User user = new User(uname, upwd, Integer.parseInt(uid), sex, email, address, mobilephone);
			
			int i = service.updateUser(user);
			
			if(i>0){
				getAllUser(request, response);
			}else{
				request.setAttribute("mes", "修改失败!");
				request.getRequestDispatcher("error.jsp").forward(request, response);
			}
		}
		//修改宠物信息
		if("updDog".equals(type)){
			String uid = request.getParameter("userId");
			String uname = request.getParameter("username");
			String age = request.getParameter("age");
			String birthday = request.getParameter("birthday");
			String type1 = request.getParameter("type");
			String color = request.getParameter("color");
			String pic = request.getParameter("pic");
			String body_length = request.getParameter("body_length");
			String body_weight = request.getParameter("body_weight");
			
			Dog dog = new Dog(Integer.parseInt(uid), uname, age, birthday, type1, color, pic, body_length, body_weight);
			
			int i = service.updateDog(dog);
			if(i>0){
				getAllDog(request, response);
			}else{
				request.setAttribute("mes", "修改失败");
				request.getRequestDispatcher("error.jsp").forward(request, response);
			}
		}
		
		
		
		
		
		
		
		
		
	}
}
